package ru.ifmo.math.support;

/**
 * @author avhaliullin
 */
public class Cyclotomic {
    static int[] a = new int[]{
            1, 2, 4, 8,
            16, 27, 13, 26,
            15, 30, 7, 14,
            28, 3, 6, 12,
            24, 11, 22, 23,
            21, 17, 25, 9,
            18, 31, 5, 10,
            20, 19, 29};

//    static int[] a = new int[]{1, 2, 4, 8, 16, 15, 30, 19, 9, 18, 11, 22, 3, 6, 12, 24, 31, 17, 13, 26, 27, 25, 29, 21, 5, 10, 20, 7, 14, 28, 23};

        static int[] c = new int[]{1, 2, 4, 8, 16};

//    static int[] c = new int[]{3, 6, 12, 24, 17};

    static int foo(int... arg) {
        int acc = 0;
        for (int i : arg) {
            acc += c[i];
        }
        return a[acc % 31];
    }

    static boolean check(int i) {
        switch (i) {
            case 5: {
                return true;
            }
            case 4: {
                return (foo(0) + foo(1) + foo(2) + foo(3) + foo(4)) % 2 == 1;
            }
            case 3: {
                return (foo(3, 4) + foo(2, 4) + foo(2, 3) + foo(1, 4) + foo(1, 3) + foo(1, 2) + foo(0, 4) + foo(0, 3) + foo(0, 2) + foo(0, 1)) % 2 == 1;
            }
            case 2: {
                return (foo(0, 1, 2) + foo(0, 1, 3) + foo(0, 1, 4) + foo(0, 2, 3) + foo(0, 2, 4) + foo(0, 3, 4) + foo(1, 2, 3) + foo(1, 2, 4) + foo(1, 3, 4) + foo(2, 3, 4)) % 2 == 1;
            }
            case 1: {
                return (foo(0, 1, 2, 3) + foo(0, 1, 2, 4) + foo(0, 1, 3, 4) + foo(0, 2, 3, 4) + foo(1, 2, 3, 4)) % 2 == 1;
            }
            case 0: {
                return (foo(0, 1, 2, 3, 4)) % 2 == 1;
            }
        }
        return false;
    }

    public static void main(String[] args) {
//        for (int i = 5; i >= 0; i--) {
//            if (check(i)) {
//                System.out.print(" x^" + i + " + ");
//            }
//        }

        for (int i = 0; i < 31; i++) {
            if ((a[(i * 2) % 31] ^ a[(i + 8) % 31] ^ a[30]) == 0) {
                System.out.println(i);
            }
        }

    }
}
